'''
Created on Mar 7, 2016

@author: JimmyMo
'''
from reader.perfsynreader import Reader
import json
import time
from database import dao
from common import commonutil

class Reader4Redis(Reader):
    def _parse_options(self):
        self.host = "localhost" 
        self.port = 6379
        self.c_db = 0
        self.p_db = 1
        self.group = "BBB"
        self.buffer_size = 200000
#         self.key = "h1"
        pass
    def __init__(self):
        super().__init__()
        self.rc_client = dao.get_redis_connection(self.host, self.port, self.c_db)
        self.rp_client = dao.get_redis_connection(self.host, self.port, self.p_db)
        pass
    def _read(self):
        now = time.clock()
#         t_d = self.rc_client.keys("GRP:CCC*")
#         self.rc_client.scan(cursor, match, count)
        cursor = 0
        started = False
#         content = [] ### TODO: need to comment out
        while cursor > 0 or not started:
            started = True
#             b = self.rc_client.scan(cursor, count=self.buffer_size)
            b = self.rc_client.scan(cursor, ("GRP:%s*" % self.group), count=self.buffer_size)
            cursor = b[0]
            keys = b[1]
            if keys:
                current_event_bytes = self.rc_client.mget(keys)
                prior_event_bytes = self.rp_client.mget(keys)
                current_event_list = list(Reader4Redis.__convert_to_list(current_event_bytes))
#                 prior_event_dict = 
                prior_event_dict = {}
#                 for i in current_event_bytes:
                prior_event_iter = Reader4Redis.__convert_to_list(current_event_bytes)
                for p_event in prior_event_iter:
                    dn = p_event['devicename']
                    key = commonutil.get_key(p_event)
#                     pn = commonutil.get_port_name(p_event, 'portname', commonutil.DEFAULT_PORTNAME)
#                     key = "%s_%s" % (dn, pn)
                    prior_event_dict[key] = p_event
                    
#                     prior_event_dict[p_event['']]
#                     print(prior_event_iter)
#                     prior_event_dict[]
#                 prior_event_list = list(Reader4Redis.__convert_to_dict(current_event_bytes))
                ###prior_event_list = list(Reader4Redis.__convert_to_list(prior_event_bytes))
#                 prior_event_list = []
                self.successor.process(current_event_list, prior_event_dict)
#                 content.extend(current_event_list) ### TODO: need to comment out
            
#         b = self.rc_client.scan
#         print(b)
        
#         print("keys fetch took %s docs spent %s..." % (len(content), (time.clock() - now)))
        
#         print("Converted to dict spent %s..." % (time.clock() - now))
#         return ls
    
#         dict_b = self.rc_client.hgetall(self.key)
#         ls = Reader4Redis.__convert_to_list(dict_b)
# #         dict_s = Reader4Redis.__decode_to_utf8(dict_b)
#         print(self.rc_client)
#         print(len(ls))
#         return ls
#         pass
#     @staticmethod
#     def __convert_to_dict(iter):
#         for it in iter:
#             it_s = it.decode('utf-8')
#             it_s = it_s.replace("'", '"')
#             d = json.loads(its)
#         
#         pass
    @staticmethod
    def __convert_to_list(iter):
        for it in iter:
            it_s = it.decode('utf-8')
            it_s = it_s.replace("'", '"')
            yield json.loads(it_s)
        pass
#     @staticmethod
#     def __convert_to_list(dict_b):
#         ls = []
#         for k,v in dict_b.items():
#             v_s = v.decode('utf-8')
#             v_s = v_s.replace("'", '"')
#             d = json.loads(v_s)
# #             d["devicename"] = k.decode('utf-8')
# #             v["devicename"] = k
#             ls.append(d)
#         return ls
#         pass
    
    @staticmethod
    def __decode_to_utf8(dict_b):
        dict_s = {}
        for k, v in dict_b.items():
            dict_s[k.decode('utf-8')] = v.decode('utf-8')
        return dict_s